package com.lagou.Stage2.module1.task2;

import java.sql.*;

public class Test {
    public static void main(String[] args) {
        //加载工具类DBUtils
        DBUtils d = new DBUtils();
        //获取连接
        Connection con = d.getConnection();
        //获取statement
        Statement stat = null;
        PreparedStatement ps = null;
        //ResultSet
        ResultSet rs = null;
        //定义Employee类引用
        Employee emp = new Employee();
        try {
            stat = con.createStatement();
            stat.executeUpdate("truncate table employee");
            ps = con.prepareStatement("INSERT INTO employee VALUES (?,?,?,?,?,?)");
            ps.setObject(1,null);
            ps.setString(2,"张三");
            ps.setString(3,"男");
            ps.setDouble(4,8888.88);
            ps.setDouble(5,1111.11);
            ps.setString(6,"2021-09-01");
            ps.executeUpdate();
            ps.setObject(1,null);
            ps.setString(2,"李四");
            ps.setString(3,"男");
            ps.setDouble(4,5555.88);
            ps.setDouble(5,2222.11);
            ps.setString(6,"2021-09-01");
            ps.executeUpdate();
            ps.setObject(1,null);
            ps.setString(2,"韩梅梅");
            ps.setString(3,"女");
            ps.setDouble(4,7777.22);
            ps.setDouble(5,1234.11);
            ps.setString(6,"2021-09-01");
            ps.executeUpdate();

            //测试查询
            ps = con.prepareStatement("select * from employee where name = ? and gender = ?");
            ps.setString(1,"张三");
            ps.setString(2,"男");
            rs = ps.executeQuery();
            while (rs.next()) {
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("NAME"));
                emp.setGender(rs.getString("gender"));
                emp.setSalary(rs.getDouble("salary"));
                emp.setBonus(rs.getDouble("bonus"));
                emp.setJoin_date(rs.getString("join_date"));
                //System.out.println(emp.getId() + ":" + emp.getName() + emp.getGender() + emp.getSalary() + emp.getBonus() + emp.getJoin_date());
                System.out.println(emp.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        d.close(con,stat,ps,rs);

    }
}
